package com.carbon.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.domain.system.vo.SysDictModelVo;
import com.carbon.system.entity.CarbonArticle;
import com.carbon.system.entity.SysDictItem;
import com.carbon.system.param.SysDictAddParam;
import com.carbon.system.param.SysDictItemAddParam;
import com.carbon.system.param.SysDictItemQueryParam;
import com.carbon.system.service.SysDictService;
import com.carbon.system.param.SysDictQueryParam;
import com.carbon.system.vo.SysDictItemQueryVo;
import com.carbon.system.vo.SysDictQueryVo;
import com.carbon.system.entity.SysDict;
import com.carbon.system.common.BaseController;
import com.carbon.domain.common.ApiResult;
import com.carbon.common.api.Paging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Wrapper;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 系统字典  前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2021-08-16
 */
@Slf4j
@RestController
@RequestMapping("/sysDict")
@Api(value = "系统字典 模块", tags = {"系统字典 模块"})
public class SysDictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;
    //search
    @PostMapping("/search")
    @ApiOperation(value = "查询系统字典")
    public ApiResult<IPage<SysDict>> search(@RequestBody @Valid SysDictQueryParam param) {
        Page<SysDict> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<SysDict> paging = sysDictService.search(page,param);
        return ApiResult.ok(paging);
    }
    @PostMapping("/searchItem")
    @ApiOperation(value = "查询系统字典")
    public ApiResult<IPage<SysDictItem>> searchItem(@RequestBody @Valid SysDictItemQueryParam param) {
        Page<SysDictItem> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<SysDictItem> paging = sysDictService.searchItem(page,param);
        return ApiResult.ok(paging);
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加系统字典")
    public ApiResult<Boolean> add(@RequestBody @Valid SysDictAddParam param) {
        SysDict sysDict = new SysDict();
        sysDict.setDictCode(param.getDictCode());
        sysDict.setDictName(param.getDictName());
        sysDict.setDescription(param.getDescription());
        sysDict.setType(param.getType());
        return ApiResult.result(sysDictService.saveB(sysDict));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新系统字典")
    public ApiResult<Boolean> update(@RequestBody @Valid SysDict sysDictQueryVo) {

        boolean update = sysDictService.updateByIdB(sysDictQueryVo);
        return ApiResult.result(update);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除系统字典")
    public ApiResult<Boolean> delete(@RequestBody @Valid SysDictItem sysDictQueryVo) {
        Integer count = sysDictService.searchItemforDelete(sysDictQueryVo.getDictCode());
        if(count>0)return ApiResult.fail("请先在字典配置里删除字典条目");
        boolean delete = sysDictService.removeItemById(sysDictQueryVo.getId());
        return ApiResult.result(delete);
    }
    @PostMapping("/addItem")
    @ApiOperation(value = "添加系统字典条目")
    public ApiResult<Boolean> addItem(@RequestBody @Valid SysDictItem param) {
        param.setStatus(1);
        Boolean a = sysDictService.saveItem(param);
        return ApiResult.result(a);
    }
    @PostMapping("updateItem")
    @ApiOperation(value = "更新系统字典条目")
    public ApiResult<Boolean> updateItem(@RequestBody @Valid SysDictItem sysDictItem) {
        Boolean a = sysDictService.updateItemById(sysDictItem);
        return ApiResult.result(a);
    }
    @PostMapping("deleteItem")
    @ApiOperation(value = "删除系统字典条目")
    public ApiResult<Boolean> deleteItem(@RequestBody @Valid SysDictItem sysDictItem) {
        Boolean a = sysDictService.removeItemByIdB(sysDictItem.getId());
        return ApiResult.result(a);
    }


}

